package com.dao;

import java.util.List;

import com.db.JdbcTemplate;
import com.entity.Article;
import com.mapping.ArticleMapping;
import com.mapping.Article_user_nameMapping;

public class ArticleDAO {
	private static JdbcTemplate jdbc = new JdbcTemplate();
	/**
	 * 新增
	 * 
	 * @param 
	 * @return
	 */
	public boolean save(Article article) {
		int r = 0;
		String sql = "insert into article(article_id,user_id,article_name,article_content)values(article_seq.nextval,?,?,?)";
		Object[] values = new Object[] { article.getUser_id(),article.getArticle_name(),
				article.getArticle_content() };
		r = jdbc.update(sql, values);
		return (r == 1);
	}
	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	public boolean delectById(int id) {
		String sql = "delete from article where article_id=" + id;
		int r = 0;
		r = jdbc.update(sql);
		return (r == 1);
	}

	public boolean delectByName(String name) {
		String sql = "delete from article where article_name=" + "'" + name + "'";
		int r = 0;
		r = jdbc.update(sql);
		return (r == 1);
	}/**
	 * 修改
	 * 
	 * @param 
	 * @return
	 */
	public boolean updateById(Article article) {
		String sql = "update article set user_id=?,article_name=?,article_content=? " + "where "
				+ "article_id=?";
		Object[] values = new Object[] { article.getUser_id(),article.getArticle_name(),
				article.getArticle_content(), article.getArticle_id() };
		int r = 0;
		r = jdbc.update(sql, values);
		return (r == 1);
	}

	public boolean updateByName(Article article) {
		String sql = "update article set user_id=?,article_content=? " + "where "
				+ "article_name=?";
		Object[] values = new Object[] { article.getUser_id(),article.getArticle_content(),
				article.getArticle_name() };
		int r = 0;
		r = jdbc.update(sql, values);
		return (r == 1);
	}

	/**
	 * 查询全部
	 * 
	 * @param booktype
	 * @return
	 */
	public List<Article> findAll() {
		String sql = "select " + "article_id,user_id,article_name,article_content " + "from "
				+ "article order by article_id asc";
		List<Article> article = null;
		article = jdbc.query(sql, new ArticleMapping());
		return article;
	}

	/**
	 * 查询一条id
	 * 
	 * @param id
	 * @return
	 */
	public Article findById(int id) {
		String sql = "select" + " article_id,user_id,article_name,article_content  " + "from  "
				+ "article where article_id=" + id;
		Article article = null;
		List entity = jdbc.query(sql, new ArticleMapping());
		article = (Article) entity.get(0);
		return article;
	}

	public Article findById_user_name(int id) {
		String sql = "select" + " a.article_id,a.user_id,u.user_name,a.article_name,a.article_content  " + "from  "
				+ "article a,users u where a.user_id=u.user_id and a.article_id=" + id;//组合sql
		Article article = null;
		List entity = jdbc.query(sql, new Article_user_nameMapping());
		article = (Article) entity.get(0);
		return article;
	}
	/**
	 * 查询一条name
	 * 
	 * @param name
	 * @return
	 */
	public Article findByName(String name) {
		String sql = "select " + "article_id,user_id,article_name,article_content " + "from "
				+ "article where article_name=" + "'" + name + "'";
		Article article = null;
		List entity = jdbc.query(sql, new ArticleMapping());
		article = (Article) entity.get(0);
		return article;
	}
	public List<Article> findByUserId(int user_id) {
		String sql ="select " + "article_id,user_id,article_name,article_content " + "from "
		+ "article where User_Id=" + user_id;
		List<Article> article = null;
		article = jdbc.query(sql, new ArticleMapping());
		return article;
	}


}
